//
//  BottomSheetView.m
//  JmzGo
//
//  Created by apple  on 2017/8/16.
//  Copyright © 2017年 金拇指. All rights reserved.
//

#import "BottomSheetView.h"


#define viewHeight ScreenH/2

@interface BottomSheetView()
{
    UIView *_contentView;
}

@end

@implementation BottomSheetView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self initContent];
    }
    
    return self;
}

- (void)initContent
{
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    
    //alpha 0.0  白色   alpha 1 ：黑色   alpha 0～1 ：遮罩颜色，逐渐
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
    
    if (_contentView == nil)
    {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenH - viewHeight, ScreenW, viewHeight)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        
        _current_ssid = [UILabel new];
        [_contentView addSubview:_current_ssid];
        [_current_ssid mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(_contentView).offset(15);
            make.leftMargin.offset(15);
            make.width.offset(ScreenW/2);
            
        }];
        
        UIView *line = [UIView new];
        line.backgroundColor = [UIColor lightGrayColor];
        [_contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(_current_ssid.mas_bottom).offset(10);
            make.width.offset(ScreenW);
            make.height.offset(1);
        }];
        
        UIView *connectView = [UIView new];
        [_contentView addSubview:connectView];
        [connectView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(line.mas_bottom).offset(10);
            make.width.offset(ScreenW/2);
            make.height.offset(viewHeight/2);
        }];
        
        UIButton *connectBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [connectBtn1 setBackgroundImage:[UIImage imageNamed:@"icon_wifi_connect"] forState:UIControlStateNormal];
        connectBtn1.tag = 1;
        [connectBtn1 addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [connectView addSubview:connectBtn1];
        [connectBtn1 mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(connectView).offset(10);
            make.centerX.equalTo(connectView.mas_centerX);
            make.width.offset(30);
            make.height.offset(30);
        }];
        
        UIButton *connectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [connectBtn setTitle:@"安全连接" forState:UIControlStateNormal];
        [connectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        connectBtn.tag = 1;
        [connectBtn setBackgroundColor:[UIColor clearColor]];
        [connectBtn addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [connectView addSubview:connectBtn];
        [connectBtn mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(connectBtn1.mas_bottom).offset(3);
            make.centerX.equalTo(connectView.mas_centerX);
            make.width.equalTo(connectView);
            make.height.offset(46);
        }];
        
        UIView *shareView = [UIView new];
        [_contentView addSubview:shareView];
        [shareView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(line.mas_top).offset(10);
            make.right.equalTo(_contentView.mas_right);
            make.width.offset(ScreenW/2);
            make.height.offset(viewHeight/2);
        }];
        
        UIButton *shareBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareBtn1 setBackgroundImage:[UIImage imageNamed:@"icon_wifi_share"] forState:UIControlStateNormal];
        shareBtn1.tag = 2;
        [shareBtn1 addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [shareView addSubview:shareBtn1];
        [shareBtn1 mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(shareView).offset(10);
            make.centerX.equalTo(shareView.mas_centerX);
            make.width.offset(30);
            make.height.offset(30);
        }];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareBtn setTitle:@"分享热点" forState:UIControlStateNormal];
        [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        shareBtn.tag = 2;
        [shareBtn setBackgroundColor:[UIColor clearColor]];
        [shareBtn addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [shareView addSubview:shareBtn];
        [shareBtn mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(shareBtn1.mas_bottom).offset(3);
            make.centerX.equalTo(shareView.mas_centerX);
            make.width.equalTo(shareView);
            make.height.offset(46);
        }];

        
    }
}



//展示从底部向上弹出的UIView（包含遮罩）
- (void)showInView:(UIView *)view withSSID:(NSString *)ssid
{
    if (!view)
    {
        return;
    }
    
    [view addSubview:self];
    [view addSubview:_contentView];
    
    _current_ssid.text = ssid;
    [_contentView setFrame:CGRectMake(0, ScreenH, ScreenW, viewHeight)];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 1.0;
        
        [_contentView setFrame:CGRectMake(0, viewHeight, ScreenW, viewHeight)];
        
    } completion:nil];
}

//移除从上向底部弹下去的UIView（包含遮罩）
- (void)disMissView
{
    [_contentView setFrame:CGRectMake(0, ScreenH - viewHeight, ScreenW, viewHeight)];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         
                         self.alpha = 0.0;
                         
                         [_contentView setFrame:CGRectMake(0, ScreenH, ScreenW, viewHeight)];
                     }
                     completion:^(BOOL finished){
                         
                         [self removeFromSuperview];
                         [_contentView removeFromSuperview];
                         
                     }];
    
}

- (void)ButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(bottomSheetAction:)]) {
        [self.delegate bottomSheetAction:(int)sender.tag];
    }
}

@end
